//
//  picPickerCollectionView.swift
//  WB
//
//  Created by leador on 17/5/15.
//  Copyright © 2017年 袁满. All rights reserved.
//

import UIKit

private let cellIdentifier = "picPickerCollectionCell"
private let edgeMargin : CGFloat = 15

class picPickerCollectionView: UICollectionView {
    
    
    var images:[UIImage] = [UIImage]() {
        
        didSet {
            reloadData()
        }
        
    }

    override func awakeFromNib() {
        
        backgroundColor = ld_RGBColor(242, 242, 242)
        
        register(UINib(nibName: "picPickerCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: cellIdentifier)
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        let itemWH = (kScreenWidth-4*edgeMargin) / 3
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumLineSpacing = edgeMargin
        layout.minimumInteritemSpacing = edgeMargin
        self.contentInset = UIEdgeInsets(top: edgeMargin, left: edgeMargin, bottom: 0, right: edgeMargin)
        
        dataSource = self
        
    }

}

extension picPickerCollectionView:UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return images.count + 1
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! picPickerCollectionViewCell
        
        cell.photoImage = indexPath.row < images.count ? images[indexPath.row] : nil
    
        return cell
        
    }
    
}
